/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package liftsimulator;

import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;
import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

/**
 *
 * @author Michael Peever & Alex Pool
 */
public class ElevatorUI extends JFrame {

    int noFloors;
    //Panel Declarations
    JPanel directionPanel = new JPanel();
    JLabel directionLbl = new JLabel();
    JPanel masterElevatorPanel = new JPanel();
    JPanel floorNumPanel = new JPanel();
    JPanel selectElevatorPanel = new JPanel();
    JPanel dirStopDoor = new JPanel();
    JPanel elevatorDirectionPanel = new JPanel();
    JPanel elevatorDoorStatus = new JPanel();
    JPanel elevatorCurrentFloorPanel = new JPanel();
    JLabel elevatorCurrentFloorLbl = new JLabel();
    JPanel elevatorCurrentDoorPanel = new JPanel();
    JLabel elevatorCurrentDoorLbl = new JLabel();
    JPanel elevatorUsersPanel = new JPanel();
    JLabel elevatorUsersLbl = new JLabel();
    JPanel elevatorAlarmPanel = new JPanel();
    JButton elevatorAlarmBttn = new JButton();
    //button declarations
    JButton floorNumBttn;
    //required objects
    public Lift l;
    public Building b;
    //variables
    int numID;
    int alarmCounter = 1;
    String idString = "";
    String masterString = "";
    boolean alarmFlag = false;
    ImageIcon upImage = new ImageIcon("C:\\Users\\Peev\\Documents\\engineering\\third year\\Sem 2\\CP2013\\Elevator_SIM_2\\LiftSimulator151011\\src\\images\\UP.jpg");
    ImageIcon downImage = new ImageIcon("C:\\Users\\Peev\\Documents\\engineering\\third year\\Sem 2\\CP2013\\Elevator_SIM_2\\LiftSimulator151011\\src\\images\\DOWN.jpg");
    ImageIcon openImage = new ImageIcon("C:\\Users\\Peev\\Documents\\engineering\\third year\\Sem 2\\CP2013\\Elevator_SIM_2\\LiftSimulator151011\\src\\images\\OPEN.jpg");
    ImageIcon closedImage = new ImageIcon("C:\\Users\\Peev\\Documents\\engineering\\third year\\Sem 2\\CP2013\\Elevator_SIM_2\\LiftSimulator151011\\src\\images\\CLOSED.jpg");
    ImageIcon aclosedImage = new ImageIcon("C:\\Users\\Peev\\Documents\\engineering\\third year\\Sem 2\\CP2013\\Elevator_SIM_2\\LiftSimulator151011\\src\\images\\ACLOSED.jpg");
    ImageIcon aopenImage = new ImageIcon("C:\\Users\\Peev\\Documents\\engineering\\third year\\Sem 2\\CP2013\\Elevator_SIM_2\\LiftSimulator151011\\src\\images\\AOPEN.jpg");
    ImageIcon alarmImage = new ImageIcon("C:\\Users\\Peev\\Documents\\engineering\\third year\\Sem 2\\CP2013\\Elevator_SIM_2\\LiftSimulator151011\\src\\images\\ALARM.jpg");

    public ElevatorUI(int noFloors, Lift l, Building b) {

        this.setSize(320, 320);

        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setVisible(true);

        this.add(masterElevatorPanel);
        this.noFloors = noFloors;
        this.l = l;
        this.b = b;
        createFloorNumPanel();
        createDirectionPanel(b.ls.e.getDirection());
        createCurrentFloorNumPanel(b.ls.e.getLiftCurrentYPos());
        createAlarmPanel();
        createDoorStatusPanel(b.ls.e.getStatus());
        createUsersOnLiftPanel();

    }

    public void setID(int numID) {
        this.numID = numID;
    }

    public int getID() {
        return numID;
    }

    public void createAlarmPanel() {
        elevatorAlarmBttn.setIcon(alarmImage);
        elevatorAlarmPanel.add(elevatorAlarmBttn);
        elevatorAlarmBttn.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                alarmCounter++;
                if (alarmCounter % 2 == 0) {
                    System.out.println("Alarm active");
                    b.alarmActive();
                } else {

                    System.out.println("Alarm deactivated");
                    b.deactivateAlarm();
                }
            }
        });
        masterElevatorPanel.add(elevatorAlarmPanel);
    }

    public void updateLblPanel(String direction) {
        if (direction.equals("UP")) {
            directionLbl.setIcon(upImage);

        }
        if (direction.equals("DOWN")) {
            directionLbl.setIcon(downImage);

        }
    }

    public void updateFloorNumLbl(int currentFloor) {
        int i = currentFloor / 50;
        elevatorCurrentFloorLbl.setText(Integer.toString(currentFloor / 50));
        if (b.region.equals("AUS/UK")) {
            for (int k = 0; k < b.noFloors; k++) {
                if (i == b.noFloors - 1) {
                    elevatorCurrentFloorLbl.setText("G");
                }
                if (i == b.noFloors - k - 1) {
                    elevatorCurrentFloorLbl.setText(Integer.toString(k));
                }
                if (i == 0) {
                    elevatorCurrentFloorLbl.setText(Integer.toString(b.noFloors-1));
                }
            }
        }
        if (b.region.equals("USA")) {
            for (int k = 0; k < b.noFloors; k++) {
                if (i == b.noFloors - k) {
                    elevatorCurrentFloorLbl.setText(Integer.toString(k));
                }
                if (i == 0) {
                    elevatorCurrentFloorLbl.setText(Integer.toString(b.noFloors));
                }
            }
        }

    }

    public void updateUsersOnLiftPanel() {
        elevatorUsersLbl.setText(createMasterString());
        elevatorUsersPanel.add(elevatorUsersLbl);
    }

    public void createUsersOnLiftPanel() {
        elevatorUsersLbl.setText(createMasterString());
        elevatorUsersPanel.add(elevatorUsersLbl);
        masterElevatorPanel.add(elevatorUsersPanel);
    }

    public void addIDToStringArray(int id, int destFloor) {
        idString = "(" + "[" + Integer.toString(id) + "]F:" + Integer.toString(destFloor) + ") ";
        b.peopleOnLiftArray.add(idString);
    }

    public String createMasterString() {
        String masterString = "";
        for (int i = 0; i < b.peopleOnLiftArray.size(); i++) {
            masterString = masterString + b.peopleOnLiftArray.get(i);
        }
        return masterString;

    }

    public void removeIDFromLift(int id, int destFloor) {
        String tempString = "";
        tempString = "(" + "[" + Integer.toString(id) + "]F:" + Integer.toString(destFloor) + ") ";
        for (int i = 0; i < b.peopleOnLiftArray.size(); i++) {
            if (tempString.equals(b.peopleOnLiftArray.get(i))) {
                b.peopleOnLiftArray.remove(i);
            }
        }
    }

    public void updateDoorStatusPanel(String liftDoorStatus) {
        if (liftDoorStatus.equals("READY")) {
            elevatorCurrentDoorLbl.setIcon(openImage);
            elevatorCurrentDoorPanel.add(elevatorCurrentDoorLbl);
        }
        if (liftDoorStatus.equals("MOVING")) {
            elevatorCurrentDoorLbl.setIcon(closedImage);
            elevatorCurrentDoorPanel.add(elevatorCurrentDoorLbl);
        }
        if (liftDoorStatus.equals("CLOSING")) {
            elevatorCurrentDoorLbl.setIcon(aclosedImage);
            elevatorCurrentDoorPanel.add(elevatorCurrentDoorLbl);
        }
        if (liftDoorStatus.equals("OPENING")) {
            elevatorCurrentDoorLbl.setIcon(aopenImage);
            elevatorCurrentDoorPanel.add(elevatorCurrentDoorLbl);
        }
    }

    public void createDoorStatusPanel(String liftDoorStatus) {
        if (liftDoorStatus.equals("READY")) {
            elevatorCurrentDoorLbl.setIcon(openImage);
            elevatorCurrentDoorPanel.add(elevatorCurrentDoorLbl);
        }
        if (liftDoorStatus.equals("MOVING")) {
            elevatorCurrentDoorLbl.setIcon(closedImage);
            elevatorCurrentDoorPanel.add(elevatorCurrentDoorLbl);
        }
        if (liftDoorStatus.equals("CLOSING")) {
            elevatorCurrentDoorLbl.setIcon(aclosedImage);
            elevatorCurrentDoorPanel.add(elevatorCurrentDoorLbl);
        }
        if (liftDoorStatus.equals("OPENING")) {
            elevatorCurrentDoorLbl.setIcon(aopenImage);
            elevatorCurrentDoorPanel.add(elevatorCurrentDoorLbl);
        }
        masterElevatorPanel.add(elevatorCurrentDoorPanel);
    }

    public void createCurrentFloorNumPanel(int currentFloor) {
        elevatorCurrentFloorLbl.setText(Integer.toString(b.noFloors));
        elevatorCurrentFloorPanel.add(elevatorCurrentFloorLbl);
        elevatorCurrentFloorPanel.setBorder(BorderFactory.createTitledBorder(BorderFactory.createEtchedBorder()));
        masterElevatorPanel.add(elevatorCurrentFloorPanel);
    }

    public void createDirectionPanel(String direction) {

        if (direction.equals("UP")) {
            directionLbl.setIcon(upImage);
            directionPanel.add(directionLbl);
        }
        if (direction.equals("DOWN")) {
            directionLbl.setIcon(downImage);
            directionPanel.add(directionLbl);
        }
        masterElevatorPanel.add(directionPanel);
    }

    public void createFloorNumPanel() {
        floorNumPanel.setLayout(new GridLayout(3, 5));//work on this
        if (b.region == "AUS/UK") {
            for (int i = 0; i <= noFloors - 1; i++) {
                floorNumBttn = new JButton();
                if (i == 0) {
                    floorNumBttn.setText("G");
                } else {
                    floorNumBttn.setText(Integer.toString(i));
                }
                final int floorNum = i + 1;
                floorNumBttn.addActionListener(new ActionListener() {

                    public void actionPerformed(ActionEvent e) {
                        b.manualAddPersonToLift(numID, floorNum);

                    }
                });
                floorNumPanel.add(floorNumBttn);
            }
        } else {
            for (int i = 1; i <= noFloors; i++) {
                floorNumBttn = new JButton();
                floorNumBttn.setText(Integer.toString(i));
                final int floorNum = i;
                floorNumBttn.addActionListener(new ActionListener() {

                    public void actionPerformed(ActionEvent e) {
                        b.manualAddPersonToLift(numID, floorNum);

                    }
                });
                floorNumPanel.add(floorNumBttn);
            }
        }
        masterElevatorPanel.add(floorNumPanel);


    }
}
